package com.ruoyi.order.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.order.domain.OrderDTO;
import com.ruoyi.order.utils.IdWorker;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.order.domain.TbOrder;
import com.ruoyi.order.service.ITbOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2023-02-07
 */
@RestController
@RequestMapping("/order")
public class TbOrderController extends BaseController
{
    @Autowired
    private ITbOrderService tbOrderService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询订单列表
     */
    @RequiresPermissions("order:order:list")
    @GetMapping("/list")
    public TableDataInfo list(TbOrder tbOrder)
    {
        startPage();
        List<TbOrder> list = tbOrderService.selectTbOrderList(tbOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbOrder tbOrder)
    {
        List<TbOrder> list = tbOrderService.selectTbOrderList(tbOrder);
        ExcelUtil<TbOrder> util = new ExcelUtil<TbOrder>(TbOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @RequiresPermissions("order:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbOrderService.selectTbOrderById(id));
    }

    /**
     * 新增订单
     */
    @RequiresPermissions("order:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbOrder tbOrder)
    {
        return toAjax(tbOrderService.insertTbOrder(tbOrder));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("order:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbOrder tbOrder)
    {
        return toAjax(tbOrderService.updateTbOrder(tbOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("order:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbOrderService.deleteTbOrderByIds(ids));
    }


    /**
     * 新增订单
     */

    @PostMapping("addOrder")
    public AjaxResult addOrder(@RequestBody OrderDTO orderDTO)
    {
        return toAjax(tbOrderService.addOrder(orderDTO));
    }
    /**
     * 我的订单
     */

    @GetMapping("myOrderList")
    public List<TbOrder> myOrderList()
    {
        LoginUser loginUser = com.ruoyi.common.security.utils.SecurityUtils.getLoginUser();
        Integer cuserId = Math.toIntExact(loginUser.getUserid());
      return tbOrderService.myOrderList(cuserId);
    }

    @GetMapping("refundOrder")
    public AjaxResult refundOrder(@RequestBody OrderDTO orderDTO){
        Map orderRefund= new HashMap();
        Long cuserId = orderDTO.getCuserId();
        String totalAmount = orderDTO.getTotalAmount();
        orderRefund.put("cuserId",cuserId);
        orderRefund.put("totalAmount",totalAmount);
        //进行
        //发送一条消息到消息队列
        rabbitTemplate.convertAndSend("","queue_1",orderRefund,message -> {
            IdWorker idWorker = new IdWorker();
            message.getMessageProperties().setMessageId(String.valueOf(idWorker.nextId()));
            return message;
        });
        return AjaxResult.success("1","正在处理请注意手机消息");
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "finOrderByid")
    public TbOrder finOrderByid(@RequestParam("id") String id)
    {
        return tbOrderService.selectTbOrderById(Long.valueOf(id));
    }


}
